' Plasma LED Clock
' BASIC Anywhere Machine version by Charlie Veniot
' Based on the awesome program by b+ (https://friends-of-basic.freeforums.net/thread/207/draw-digital-numbers-plasma-clock)

_alert("At any time, click (or touch) the clock to alter the plasma colouring.")

declare function rgbf& (n1, n2, n3)
declare sub drawC (x, y, c$, c As Long)

Const xmax = 850, ymax = 200, sq = 25
Const dat$ = "1110111000001101111100011111100101110111011101101001001111111111011011"

Type xy
    x As Single
    y As Single
    dx As Single
    dy As Single
End Type
Screen _NewImage(xmax, ymax, 32)

Dim c(360) As Long, p(6) As xy, f(6)

restart:
r = Rnd: g = Rnd: b = Rnd: i = 0
For n = 1 To 5
    r1 = r: g1 = g: b1 = b
    Do: r = Rnd: Loop Until Abs(r - r1) > .2
    Do: g = Rnd: Loop Until Abs(g - g1) > .2
    Do: b = Rnd: Loop Until Abs(g - g1) > .2
    For m = 0 To 17: m1 = 17 - m
        f1 = (m * r) / 18 : f2 = (m * g) / 18 : f3 = (m * b) / 18 : c(i) = rgbf&(f1, f2, f3) : i = i + 1
    Next
    For m = 0 To 17: m1 = 17 - m
        f1 = (m + m1 * r) / 18 : f2 = (m + m1 * g) / 18 : f3 = (m + m1 * b) / 18 : c(i) = rgbf&(f1, f2, f3) : i = i + 1
    Next
    For m = 0 To 17: m1 = 17 - m
        f1 = (m1 + m * r) / 18 : f2 = (m1 + m * g) / 18 : f3 = (m1 + m * b) / 18 : c(i) = rgbf&(f1, f2, f3) : i = i + 1
    Next
    For m = 0 To 17: m1 = 17 - m
        f1 = (m1 * r) / 18 : f2 = (m1 * g) / 18 : f3 = (m1 * b) / 18 : c(i) = rgbf&(f1, f2, f3) : i = i + 1
    Next
Next

For n = 0 To 5
    p(n).x = Rnd * xmax: p(n).y = Rnd * ymax: p(n).dx = Rnd * 2 - 1: p(n).dy = Rnd * 2 - 1
    f(n) = Rnd * .1
Next

do
    For i = 0 To 5
        p(i).x = p(i).x + p(i).dx
        If p(i).x > xmax Or p(i).x < 0 Then p(i).dx = -p(i).dx
        p(i).y = p(i).y + p(i).dy
        If p(i).y > ymax Or p(i).y < 0 Then p(i).dy = -p(i).dy
    Next
    For y = 0 To ymax - 1 Step 2
        For x = 0 To xmax - 1 Step 2
            d = 0
            For n = 0 To 5
                dx = x - p(n).x: dy = y - p(n).y
                k = Sqr(dx * dx + dy * dy)
                d = d + (Sin(k * f(n)) + 1) / 2
            Next n: d = d * 60
            Line (x, y)-Step(2, 2), c(d), BF
        Next
    Next
    For j = 1 To 3
        If j = 1 Then
            c& = &HFFFFFF: offset = -2
        ElseIf j = 2 Then
            c& = &H555555: offset = 2
        Else
            c& = &HC0C0C0: offset = 0
        End If
        For n = 1 To 8 'clock digits over background
            If Mid$(Time$, n, 1) = ":" Then
                Line ((n - 1) * 4 * sq + 2 * sq + offset, sq + sq + offset)-Step(sq, sq), c&, BF
                Line ((n - 1) * 4 * sq + 2 * sq + offset, sq + 4 * sq + offset)-Step(sq, sq), c&, BF
            Else
                drawC ( int( (n - 1) * 4 * sq + sq + offset ), _
								        int( sq + offset ), _
												[Mid$(dat$, Val(Mid$(Time$, n, 1)) * 7 + 1, 7) ], _
												c& )
            End If
        Next
    Next
    _Display
loop until _mousebutton = 1
goto restart

Function rgbf& (n1, n2, n3)
    rgbf& = _RGB32(int(n1 * 256), int(n2 * 256), int(n3 * 256))
End Function

Sub drawC (x, y, c$, c As Long)
    For m = 1 To 7
        If Val(Mid$(c$, m, 1)) Then
            Select Case m
                Case 1: Line (x, y)-Step(sq, 3 * sq), c, BF
                Case 2: Line (x, y + 2 * sq)-Step(sq, 4 * sq), c, BF
                Case 3: Line (x, y)-Step(3 * sq, sq), c, BF
                Case 4: Line (x, y + 2 * sq)-Step(3 * sq, sq), c, BF
                Case 5: Line (x, y + 5 * sq)-Step(3 * sq, sq), c, BF
                Case 6: Line (x + 2 * sq, y)-Step(sq, 3 * sq), c, BF
                Case 7: Line (x + 2 * sq, y + 2 * sq)-Step(sq, 4 * sq), c, BF
            End Select
        End If
    Next
End Sub